﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="SooritaKanne_v3", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(SooritaKanneV3))]
    public class SooritaKanneV3Serializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected ISooritaKanneV3 clazz;
        
        public SooritaKanneV3Serializer(ISooritaKanneV3 clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("kande_id");
            if (clazz.KandeId!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.KandeId))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : KandeId");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("kandeliik");
            if (clazz.Kandeliik!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Kandeliik))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Kandeliik");
            }
            Writer.WriteEndElement();
            if (clazz.Seisund!=null) {
                Writer.WriteStartElement("seisund");
                global::Xtee.Core.SerializationMetadata metaSeisund = this.GetMetaData(clazz.Seisund);
                Writer.WriteTypeAttribute(metaSeisund.TypeQualifiedName.Namespace, metaSeisund.TypeQualifiedName.Name);
                metaSeisund.NewSerializer(clazz.Seisund).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            if (clazz.EelmineLahend!=null) {
                Writer.WriteStartElement("eelmine_lahend");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.EelmineLahend))).ToString());
                Writer.WriteEndElement();
            }
            Writer.WriteStartElement("kande_kuupaev");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "date");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDate(((System.DateTime)(clazz.KandeKuupaev))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("ametitoiming");
            if (clazz.Ametitoiming!=null) {
                global::Xtee.Core.SerializationMetadata metaAmetitoiming = this.GetMetaData(clazz.Ametitoiming);
                Writer.WriteTypeAttribute(metaAmetitoiming.TypeQualifiedName.Namespace, metaAmetitoiming.TypeQualifiedName.Name);
                metaAmetitoiming.NewSerializer(clazz.Ametitoiming).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Ametitoiming");
            }
            Writer.WriteEndElement();
            for (int x = 0; ((clazz.Riigiloivud != null) 
                        && (x < clazz.Riigiloivud.Count)); x = (x + 1)) {
                if (clazz.Riigiloivud[x]!=null) {
                    Writer.WriteStartElement("riigiloivud");
                    global::Xtee.Core.SerializationMetadata metaRiigiloivud = this.GetMetaData(clazz.Riigiloivud[x]);
                    Writer.WriteTypeAttribute(metaRiigiloivud.TypeQualifiedName.Namespace, metaRiigiloivud.TypeQualifiedName.Name);
                    metaRiigiloivud.NewSerializer(clazz.Riigiloivud[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            if (clazz.KandeSisu!=null) {
                Writer.WriteStartElement("kande_sisu");
                global::Xtee.Core.SerializationMetadata metaKandeSisu = this.GetMetaData(clazz.KandeSisu);
                Writer.WriteTypeAttribute(metaKandeSisu.TypeQualifiedName.Namespace, metaKandeSisu.TypeQualifiedName.Name);
                metaKandeSisu.NewSerializer(clazz.KandeSisu).WriteXml(Writer);
                Writer.WriteEndElement();
            }
            if (clazz.Markus!=null) {
                Writer.WriteStartElement("markus");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Markus))).ToString());
                Writer.WriteEndElement();
            }
            for (int x = 0; ((clazz.Dokumendid != null) 
                        && (x < clazz.Dokumendid.Count)); x = (x + 1)) {
                if (clazz.Dokumendid[x]!=null) {
                    Writer.WriteStartElement("dokumendid");
                    global::Xtee.Core.SerializationMetadata metaDokumendid = this.GetMetaData(clazz.Dokumendid[x]);
                    Writer.WriteTypeAttribute(metaDokumendid.TypeQualifiedName.Namespace, metaDokumendid.TypeQualifiedName.Name);
                    metaDokumendid.NewSerializer(clazz.Dokumendid[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            if (clazz.RegistriPiirkond!=null) {
                Writer.WriteStartElement("registri_piirkond");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.RegistriPiirkond))).ToString());
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="kande_id") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v3>kande_id");
                }
                clazz.KandeId = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("kande_id")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKanne_v3>kande_id", Reader.LocalName);
            }
            if (Reader.LocalName=="kandeliik") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v3>kandeliik");
                }
                clazz.Kandeliik = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("kandeliik")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKanne_v3>kandeliik", Reader.LocalName);
            }
            if (Reader.LocalName=="seisund") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v3>seisund");
                }
                global::Xtee.Core.SerializationMetadata metaSeisund = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISeisundType>(Reader, "seisundType", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.Seisund = metaSeisund.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISeisundType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("seisund");
                    metaSeisund.NewSerializer(clazz.Seisund).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="eelmine_lahend") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v3>eelmine_lahend");
                }
                clazz.EelmineLahend = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("eelmine_lahend")).Value;
            }
            if (Reader.LocalName=="kande_kuupaev") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v3>kande_kuupaev");
                }
                clazz.KandeKuupaev = new global::Xtee.Core.Schema.XsdDate(Reader.ReadElementString("kande_kuupaev")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKanne_v3>kande_kuupaev", Reader.LocalName);
            }
            if (Reader.LocalName=="ametitoiming") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v3>ametitoiming");
                }
                global::Xtee.Core.SerializationMetadata metaAmetitoiming = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneAmetitoiming>(Reader, "sooritakanne_ametitoiming", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.Ametitoiming = metaAmetitoiming.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneAmetitoiming>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ametitoiming");
                    metaAmetitoiming.NewSerializer(clazz.Ametitoiming).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SooritaKanne_v3>ametitoiming", Reader.LocalName);
            }
            if (Reader.LocalName=="riigiloivud") {
                clazz.Riigiloivud = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivTypeV3>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "riigiloivud")); ) {
                    if ((Reader.IsElementNil() == false)) {
                        global::Xtee.Core.SerializationMetadata metaRiigiloivud = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivTypeV3>(Reader, "riigiloivType_v3", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                        global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivTypeV3 item = metaRiigiloivud.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IRiigiloivTypeV3>();
                        clazz.Riigiloivud.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            Reader.ReadStartElement("riigiloivud");
                            metaRiigiloivud.NewSerializer(item).ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    else {
                        clazz.Riigiloivud.Add(null);
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="kande_sisu") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v3>kande_sisu");
                }
                global::Xtee.Core.SerializationMetadata metaKandeSisu = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneKandesisuV3>(Reader, "sooritakanne_kandesisu_v3", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                clazz.KandeSisu = metaKandeSisu.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ISooritakanneKandesisuV3>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("kande_sisu");
                    metaKandeSisu.NewSerializer(clazz.KandeSisu).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            if (Reader.LocalName=="markus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v3>markus");
                }
                clazz.Markus = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("markus")).Value;
            }
            if (Reader.LocalName=="dokumendid") {
                clazz.Dokumendid = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IDokumentTypeV3>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "dokumendid")); ) {
                    if ((Reader.IsElementNil() == false)) {
                        global::Xtee.Core.SerializationMetadata metaDokumendid = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IDokumentTypeV3>(Reader, "dokumentType_v3", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                        global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IDokumentTypeV3 item = metaDokumendid.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IDokumentTypeV3>();
                        clazz.Dokumendid.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            Reader.ReadStartElement("dokumendid");
                            metaDokumendid.NewSerializer(item).ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    else {
                        clazz.Dokumendid.Add(null);
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="registri_piirkond") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SooritaKanne_v3>registri_piirkond");
                }
                clazz.RegistriPiirkond = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("registri_piirkond")).Value;
            }
        }
    }
}
